define(function (require, exports) {
	var pubjs = require('pubjs');
	var $ = require('jquery');
	var grid = require('@base/highGrid');
	var util = require('util');

	// 资质审核列表
	var Blacklist = grid.base.extend({
		init: function (config) {
			config = pubjs.conf(config, {
				url: 'blacklist/list',
				// 移除url
				urlRemove: 'blacklist/remove',
				reqType: 'websocket',
				hasAmount: false,
				hasExport: false,
				hasPager: true,
				hasRefresh: true,
				hasSwitch: false,
				hasFilter: true,
				hasTab: false,
				hasMenu: true,
				hasSelect: true,
				auto_load: true,
				cols: [
					{name: '_id', sort: true, text: LANG("ID"), align: 'center', width: 60},
					{name: "BlackName", text: LANG("黑名单名称"), align: 'left', width: 220, render: 'renderBlackName'},
					{name: "Content", text: LANG("内容"), align: 'left', width: 350, render: 'renderContent'},
					{name: "CreateTime", text: LANG("创建时间"), align: 'center', width: 200, render: 'renderCreateTime'},
				],
				metrics: [

					{name: "AdvertiserId", text: LANG("广告主"), align: 'left', render: 'renderAdvertiser'}
				],
				param: {
					order: 'CreateTime|-1'
				},
				gridName: 'blacklist',
				vModel: {
					ids: []
				}
			});

			this.Super('init', arguments);
		},
		afterBuild: function () {
			var vm = this.vm;

			this.Super('afterBuild', arguments);
		},
		renderBlackName: function (i, val, row, con) {
			var icon = [
				['running', LANG('开启')],
				['close', LANG('关闭')],
				[],
				['deleted', LANG('归档')],
			];

			var num = 0;
			switch (row.Status){
				case 1:
					num = 0;
					break;
				case 2:
					num = 1;
					break;
			}
			if (row.IsDeleted) {num = 3}
			var status = icon[num];

			var href = '#admin/blacklist/edit/' + row._id;
			var html = $([
				'<div class="dib valign-m ">',
					'<a class="valign-m dib uk-text-truncate" target="_self" href="'+href+'">',
						row.Name,
					'</a>',
				'</div>'
			].join(''));

			html.find('a').width(con.width - 25);

			html = $('<div class="tl fl mr10" title="'+ row.Name +'"><i title="'+status[1]+'" class="status status-'+status[0]+' mr5"></i>'+'</div>').append(html);
			return html
		},
		renderContent: function (i, val, row, con) {

			var res = [];
			if(row.Domains && row.Domains.length){
				res.push(LANG('域名：')+'('+ row.Domains.length +')');
			}
			if(row.AppIds && row.AppIds.length){
				res.push(LANG('APPID：')+'('+ row.AppIds.length +')');
			}
			if(row.Spots && row.Spots.length){
				res.push(LANG('媒体广告位：')+'('+ row.Spots.length +')');
			}

			return res.join('；');
		},
		renderCreateTime: function (i, val, row, con) {
			var html = util.date('Y年m月d日',row.CreateTime);
			return html;
		},
		renderAdvertiser: function (i, val, row, con) {
			var arr = [];
			if(row.Advertiser && row.Advertiser.length){
				for (var i = 0; i < row.Advertiser.length; i++) {
					arr.push(row.Advertiser[i]['name']);
				}
			}

			var html = arr.join('，');

			var div = $('<div class="text-truncate"/>').text(html).attr('title', html).css('max-width', 500);

			return div;
		},
		// 批量操作-下拉菜单界面
		onBatchShow: function(ev){
			// TODO: 根据权限展示弹出菜单
			// TODO: 根据选中内容展示弹出菜单

			var list = [
				{id: 'open', name: LANG('开启')},
				{id: 'close', name: LANG('关闭')},
				// {id: 'selectAdvertiserId', name: LANG('广告主选择')},
			];

			ev.returnValue = list;
			return false;
		},
		// 批量操作-响应菜单操作
		onBatchSelect: function(ev){
			var data = ev.param[0];
			var ids = ev.param[1];
			var contents = ev.param[2];
			var key = data.key || '';
			var channelType = 0;	// 活动类型

			if(!ids || !ids.length) {
				pubjs.alert(LANG('请先选择黑名单'));
				return false;
			}

			var self = this;
			switch(key){
				case 'open':
					self.send('blacklist/open', {'id': ids });
				break;
				case 'close':
					self.send('blacklist/close', {'id': ids });
				break;
			}
			return false;
		},
		// 操作菜单-下拉菜单界面
		onOperateMenuShow: function(ev){
			var param = ev.param;
			var result = [];


			if (!param.IsDeleted) {
				result.push({id: 'edit', name: LANG('编辑')});
				if (param.Status == 1) {
					result.push({id: 'close', name: LANG('关闭')});
				}else{
					result.push({id: 'open', name: LANG('开启')});
				}

				result.push({id: 'remove', name: LANG('归档')});

				result.push({id: 'saveas', name: LANG('复制')});
			}else{
				result.push({id: 'recover', name: LANG('还原')});
			}

			ev.returnValue = result;
			return false;
		},
		// 操作菜单-响应菜单操作
		onOperateMenuSelect: function(ev){
			var op = ev.param[0];
			var data = ev.param[1];
			var c = this.getConfig();
			var menu = ev.from.getDOM();
			var id = +data._id;
			var self = this;

			switch(op.key){
				case 'edit':
					var href = 'admin/blacklist/edit/';
					pubjs.controller.navigate(href + id);
				break;
				case 'saveas':
					var href = 'admin/blacklist/saveas/';
					pubjs.controller.navigate(href + id);
				break;
				case 'close':
					self.send('blacklist/close', {'id': id });
				break;
				case 'open':
					self.send('blacklist/open', {'id': id });
				break;
				case 'remove':
					pubjs.confirm(LANG('确定归档该条记录？'), function(isOk){
						if(isOk){
							self.send('blacklist/remove', {'id': id });
						}
					});

				break;
				case 'recover':
					self.send('blacklist/recover', {'id': id });
				break;
			}
			return false;
		},
		send: function(url, param, cbName)
		{
			if(url){
				pubjs.loading.show();
				var self = this;
				pubjs.mc.send(url, param, function(err) {
					pubjs.loading.hide();
					if(err){
						return pubjs.alert(err.message);
					}

					pubjs.alert(LANG('操作成功'));
					self[cbName || 'reload'].apply(self, arguments);
				});
			}
		}
	})
	exports.base = Blacklist;
});